import { notification } from 'antd'
// 导入history实现组件外跳路由
import { createBrowserHistory } from 'history';
// 1、导入axios
import axios from "axios";

// 实例化history对象
var history = createBrowserHistory()
// 2.实例化axios
var service = axios.create({

  baseURL: '', //设置基准路径
  timeout: 10 * 1000  //设置延时时间间隔

})
// 3、设置请求拦截器
service.interceptors.request.use((config) => {
  //   设置请求头
  config.headers['Authorization'] = 'Bearer' + " " + localStorage.getItem('token');
  return config;
}, (error) => {
  return Promise.reject(error);
})
//4、设置响应拦截器
service.interceptors.response.use((config) => {
  return config;
}, (error) => {
  //处理请求错误码
  if (error.response.status == 401) {
    notification.error({ message: '登录过期，请重新登陆！' })
    history.push('/login');
    history.go();
    localStorage.removeItem('token');//删除token
    localStorage.removeItem('userinfo');
  } else if (error.response.status == 404) {
    notification.error({ message: '请求路径错误！' })
  } else if (error.response.status == 500) {
    notification.error({ message: '服务器内部错误' })
  }
  return Promise.reject(error);
})
export default service;